package com.yc.net;

import com.google.gson.Gson;
import com.ly.common.jdbc.JdbcTemplate;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ZpwSpider {

    public static void main(String[] args) throws IOException {
        ZpwSpider zs = new ZpwSpider();
        // zs.capJob("https://www.0734zpw.com/j41548.html");
        for (int i = 1; i <= 5; i++) {
            zs.capJobs("https://www.0734zpw.com/jobs?page=" + i);
        }

    }

    // url 只能  get 请求  =>  该方法只做参考
    public void capJobs(String address, int page) throws IOException {
        URL url = new URL(address);
        URLConnection urlConnection = url.openConnection();
        InputStream in = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        StringBuffer sb = new StringBuffer();
        char[] chars = new char[1024];
        int count;
        while((count = isr.read(chars))>0){
            sb.append(chars, 0, count);
        }
        ArrayList arrayList = new Gson().fromJson(sb.toString(), ArrayList.class);
        System.out.println(arrayList);
    }

    // 手机 job 链接
    public void capJobs(String address) throws IOException {
        URL url = new URL(address);
        Document document = Jsoup.parse(url, 5000);
        Elements as = document.select(".td-j-name>a");
        for (Element element : as) {
            // /j41548.html
            String href = element.attr("href");
            String jobAddress = url.getProtocol() + "://" + url.getHost() + href;
            capJob(jobAddress);
        }
    }

    public void capJob(String address) throws IOException {
        String id = address.replaceAll(".*j(\\d+)\\.html", "$1");
        List<Map<String, Object>> list = JdbcTemplate.selectListMap(
                "select count(*) cnt from job where id=?", id);
        if(Integer.parseInt(""+list.get(0).get("cnt")) > 0){
            System.out.println("该 job " + id + " 已经采集过了");
            return;
        }

        Document document = Jsoup.parse(new URL(address), 5000);

        Element element = document.selectFirst(".jobname>.j-n-txt");
        String jobname = element.text();
        element = document.selectFirst(".jobstit>.wage");
        String money = element.text();

        Elements elements = document.select(".main>.item>.itemli");
        String type = elements.get(0).text();  //学历要求 高中
        type = type.replaceAll("学历要求(.+)", "$1");

        String age = elements.get(1).text();  //年龄要求 高中
        age = age.replaceAll("年龄要求(.+)", "$1");

        element = document.selectFirst(".main>.describe>.df2");
        String describe = element.html();

        System.out.println("jobname = " + jobname);
        System.out.println("money = " + money);
        System.out.println("type = " + type);
        System.out.println("age = " + age);
        System.out.println("describe = " + describe);

        String sql = "insert into job values(?,?,?,?,?,?)";
        JdbcTemplate.update(sql, id, jobname, money, type, age, describe);
    }


}
